﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using WebStore.Domain.Entities;
using WebStore.Domain.Entities.Enum;
using WebStore.Domain.Entities.Model;
using WebStore.Infrastructure.Service.Interface;

namespace WebStore.Infrastructure.Service.Implementation
{
    public class OrderService:IOrderService 
    {
        private readonly Entities _entities;

        public OrderService
            (
            Entities entities
            )
        {
            _entities = entities;
        }

        public bool Checkout(int userId,IEnumerable<KeyValuePair<int, int>> orderDetails)
        {
            var order = new Order
                            {
                                UserId = userId,
                                OrderStatus = OrderStatus.Waiting,
                                OrderDate = DateTime.Now,
                                OrderDetails = orderDetails.Select(x => Mapper.Map<OrderDetail>(x)).ToList()
                            };
            _entities.Orders.Add(order);
            _entities.SaveChanges();
            return true;
        }
    }
}
